package com.example.mq.mqserver.core;

import com.example.mq.common.Consumer;
import com.example.mq.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/*
 * 存储消息的队列
 * MSG --> Message
 * */
public class MSGQueue {
    // 表示队列的身份标识
    private String name;
    // 表示队列是否是持久化的
    private boolean durable = false;
    // 表示队列是否是独占的,false表示可以被多个消费者消费,true表示只能被一个消费者消费
    private boolean exclusive = false;
    // 表示队列是否是自动删除的,false表示不自动删除,true表示自动删除，如何实现？
    private boolean autoDelete = false;
    // 表示扩展参数，如何实现？
    private Map<String,Object> arguments= new HashMap<>();
    // 当前队列那些消费者订阅
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();
    // 记录当前取到了第几个消费者，方便轮询
    private AtomicInteger consumeSeq = new AtomicInteger();

    // 添加新的订阅者
    public void addConsumerEnv(ConsumerEnv consumerEnv){
        consumerEnvList.add(consumerEnv);
    }
    // 删除订阅者，暂时不考虑
//    public void removeConsumerEnv(ConsumerEnv consumerEnv){
//        consumerEnvList.remove(consumerEnv);
//    }
    // 挑选一个订阅者，用来处理当前消息，轮询的方式
    public ConsumerEnv chooseConsumer(){
        if(consumerEnvList.isEmpty()){
            return null;
        }
        // 计算当前元素要去的下标
        int index = consumeSeq.get() % consumerEnvList.size();
        consumeSeq.getAndIncrement();
        return consumerEnvList.get(index);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    public String getArguments() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }

    public void setArguments(String argumentsJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String,Object>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public void setArguments(String key,Object value) {
        this.arguments.put(key,value);
    }

    public Object getArguments(String key) {
        return this.arguments.get(key);
    }

    public void setArguments(Map<String,Object> arguments) {
        this.arguments = arguments;
    }
}
